<template>
    <van-nav-bar
        title="修改收货地址"
        left-text="返回"
        left-arrow
        @click-left="onClickLeft"
    />

    <van-address-edit
        :area-list="areaList"
        show-set-default
        show-delete
        :address-info="addressInfo"
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        @save="onEdit"
        @delete="onDelete"
       
    />
</template>

<script>
import { areaList } from '@vant/area-data'
export default {
    created() {
        let addrid = this.$route.query.addrid

        
        // console.log(addrid)

        this.addrid = addrid

        this.info()
    },
    data() {
        return {
            addrid:0,
            addressInfo:{},
            areaList,
            LoginUser:this.$cookies.get('LoginUser')
            
        }
    },
    methods: {
        
        onClickLeft()
        {
            this.$router.go(-1)
        },
        async onEdit(item)
        {
            item.id = this.addrid

            item.userid = this.LoginUser.id
            
            let result = await this.$api.AddressEdit(item)

            // console.log(result)
            if(result.code === 0)
            {
                this.$notify({
                    type: "danger",
                    message: result.msg,
                    duration:1500
                });
                return false
            }

        },
        
        onDelete()
        {
            let data = {
                addrid:this.addrid
            }
            this.$dialog.confirm({
                title: '确认删除？',
                message:'您确认删除当前收货地址'
            })
            .then( async () => {
               let result = await this.$api.AddressDel(data)
			
                if(result.code === 1)
                {
                    this.$notify({
                        type: "success",
                        message: result.msg,
                        duration:1500,
                        onClose:() => {
                            this.$router.push(result.url)
                        }
                    });
                    return false
                }else{
                    this.$notify({
                        type: "danger",
                        message: result.msg,
                        duration:1500
                    });
                    return false
                }
            })
            .catch(() => {
                // on cancel
            });
            
        },
       
        async info()
        {
            let data = {
                addrid:this.addrid
            }

            let result = await this.$api.AddressInfo(data)

            if(result.code === 0)
            {
                this.$notify({
                    type: "danger",
                    message: result.msg,
                    duration:1500
                });
                return false
            }

            let addressInfo = {
                name:result.data.consignee,
                tel:result.data.mobile,
                addressDetail:result.data.address,
                isDefault:result.data.status == 1 ? true : false
            }

            if(result.data.province)
            {
                addressInfo.province = result.data.provinces.name
                addressInfo.areaCode = result.data.province.toString()
            }

            if(result.data.city)
            {
                addressInfo.city = result.data.citys.name
                addressInfo.areaCode = result.data.city.toString()
            }

            if(result.data.district)
            {
                addressInfo.county = result.data.districts.name
                addressInfo.areaCode = result.data.district.toString()
            }

            this.addressInfo = addressInfo


        }
    },
}
</script>